package com.gw.controller;

import com.gw.context.BaseContext;
import com.gw.pojo.dto.PostDTO;
import com.gw.pojo.dto.PostSADTO;
import com.gw.pojo.dto.ReplyDTO;
import com.gw.pojo.vo.PostDetailVO;
import com.gw.pojo.vo.PostVO;
import com.gw.result.PageResult;
import com.gw.result.Result;
import com.gw.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;


    /**
     * 上传问答贴
     * @param postDTO
     * @return
     */
    @PostMapping( "/addPost")
    public Result addPost(@RequestBody PostDTO postDTO) {
        //新的方式，其中的附件由统一接口上传
        //检查标题和详情是否为空
        if (postDTO.getTitle() == null || postDTO.getTitle().isEmpty()) {
            throw new RuntimeException("写入信息有误，请检查!");
        }
        log.info("用户{}发布帖子", BaseContext.getCurrentUserId());
        postService.addPost(postDTO);
        return Result.success();
    }

    /**
     * 评论回复
     * @param replyDTO
     * @return
     */
    @PostMapping("/addReply")
    public Result addReply(@Validated @RequestBody ReplyDTO replyDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            throw new RuntimeException("传入参数有误，请检查!");
        }
        postService.addReply(replyDTO);
        return Result.success();
    }

    /**
     * 分页查询帖子
     */
    @GetMapping("/pagePost")
    public Result<PageResult> listPost(int page, int size){

        PageResult pageResult = postService.pagePost(page, size);

        return Result.success(pageResult);
    }

    /**
     * 查询某贴详情
     * @param postId
     * @return
     */
    @GetMapping("/getDetail")
    public Result<PostDetailVO> getPostDetail(long postId){
        PostDetailVO postDetailVO = postService.getDetail(postId);
        return Result.success(postDetailVO);
    }



}
